/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.ui.referencesystem;

import info.xmlprint.core.project.PrintProject;
import info.xmlprint.core.referencesystem.AbstractBaseGroup;
import info.xmlprint.core.referencesystem.PageMaster;
import info.xmlprint.core.referencesystem.PageMasterGroup;
import info.xmlprint.core.referencesystem.AbstractBaseItem;
import info.xmlprint.core.referencesystem.ReferenceFieldGroup;
import info.xmlprint.run.Constants;
import info.xmlprint.run.RuntimeUtil;
import info.xmlprint.ui.message.GuiMessages;
import info.xmlprint.ui.widgets.IObserverControl;
import info.xmlprint.ui.widgets.ISetupComposite;
import info.xmlprint.ui.widgets.ISubjectControl;
import info.xmlprint.ui.widgets.SpinnerAndCombo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

public class PageMasterComposite extends Composite implements
	ISubjectControl, ISetupComposite {
	
	private List<IObserverControl> observerList = new ArrayList<IObserverControl>();

	private Combo pageSizeCombo;
	//private StackLayout stackLayout = new StackLayout();

	/*private String[] listItemNames = new String[] {
			GuiMessages.Stileditor_PageLayout_Format,
			GuiMessages.Stileditor_PageLayout_Margins,
			GuiMessages.Stileditor_PageLayout_Layout };*/

	private String[] counterPageItem = new String[] { GuiMessages.Fieldinformation_PageNumber,
	"${pagenumber}" };
	private String[] counterColumnItem = new String[] { GuiMessages.Fieldinformation_ColumnNumber,
	"${colnumber}" };
	private String[] counterRowItem = new String[] { GuiMessages.Fieldinformation_LineNumber,
	"${linenumber}" };

	private PageMasterComposite _this = this;
	private PrintProject printProject;

	private SpinnerAndCombo heightSpinnerCombo, widthSpinnerCombo,
			obenSpinnerCombo, untenSpinnerCombo, linksSpinnerCombo,
			rechtsSpinnerCombo, bundstegSpinnerCombo,
			bundstegpositionSpinnerCombo;
	private Button doublesidedCheckbox;
	private Label labelL, labelR;
	private Group nameGroup, evengroup, oddgroup;
	private Text nameText;
	//private Cursor cursor;
	
	private Text lohead, cohead, rohead, lofoot, cofoot, rofoot, lehead, cehead, rehead, lefoot, cefoot, refoot;
	private Text[] headfootpositions; 
	
	private Text activeTextField;
	private Integer activeTextFieldPos;
	
	private org.eclipse.swt.widgets.List refFieldItemList, counterItemList_typographic;
	
	private Map<Integer, String[]> refFieldItemMap = new HashMap<Integer, String[]>();
	private Map<Integer, String[]> counterItemMap_typographic = new HashMap<Integer, String[]>();
	
	//private Shell pageLayoutSetupShell;

	//private TreeViewer treeViewer;
	
	private PageMaster pageMaster;
	private PageMasterGroup pageMasterGroup;
	
	public PageMasterComposite (org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		this.setLayout(new GridLayout());
		createControls();
	}

	private void createControls() {
	{
		nameGroup = new Group(this,SWT.NONE);
		nameGroup.setLayout(new GridLayout(2, false));
		nameGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Label name = new Label(nameGroup, SWT.NONE);
		name.setText("Name");
		
		nameText = new Text(nameGroup, SWT.BORDER);
		nameText.setToolTipText("Geben Sie einen Namen für dieses Feld an"); ////$NON-NLS-N$
		nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	}
	
	{ 
		Group outerGroup = new Group(this, SWT.NONE);
		outerGroup.setLayout(new GridLayout(2, false));
		
		Group group = new Group(outerGroup, SWT.TOP);
		group.setText(GuiMessages.Stileditor_PageLayout_Format);
		group.setLayout(new GridLayout(2, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		pageSizeCombo = new Combo(group, SWT.NONE);
		pageSizeCombo.setItems(new String[] { "A0", "A1", "A2", "A3", "A4",
				"A5", "B4", "B5",
				GuiMessages.Stileditor_PageLayout_UserdefinedFormat });
		{
			GridData gd = new GridData();
			gd.horizontalSpan = 2;
			pageSizeCombo.setLayoutData(gd);
		}

		pageSizeCombo.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				String size = pageSizeCombo.getItem(pageSizeCombo
						.getSelectionIndex());
				if (size.equalsIgnoreCase("a0")) {
					heightSpinnerCombo.setValue(Constants.A0_H);
					widthSpinnerCombo.setValue(Constants.A0_W);
				} else if (size.equalsIgnoreCase("a1")) {
					heightSpinnerCombo.setValue(Constants.A1_H);
					widthSpinnerCombo.setValue(Constants.A1_W);
				} else if (size.equalsIgnoreCase("a2")) {
					heightSpinnerCombo.setValue(Constants.A2_H);
					widthSpinnerCombo.setValue(Constants.A2_W);
				} else if (size.equalsIgnoreCase("a3")) {
					heightSpinnerCombo.setValue(Constants.A3_H);
					widthSpinnerCombo.setValue(Constants.A3_W);
				} else if (size.equalsIgnoreCase("a4")) {
					heightSpinnerCombo.setValue(Constants.A4_H);
					widthSpinnerCombo.setValue(Constants.A4_W);
				} else if (size.equalsIgnoreCase("a5")) {
					heightSpinnerCombo.setValue(Constants.A5_H);
					widthSpinnerCombo.setValue(Constants.A5_W);
				} else if (size.equalsIgnoreCase("b4")) {
					heightSpinnerCombo.setValue(Constants.B4_H);
					widthSpinnerCombo.setValue(Constants.B4_W);
				} else if (size.equalsIgnoreCase("b5")) {
					heightSpinnerCombo.setValue(Constants.B5_H);
					widthSpinnerCombo.setValue(Constants.B5_W);
				}
			}

		});

		// breite
		{
			Label label = new Label(group, SWT.NONE);
			label.setText(GuiMessages.Stileditor_PageLayout_Width + ":");
			GridData gd = new GridData();
			label.setLayoutData(gd);
		}

		widthSpinnerCombo = new SpinnerAndCombo(group, SWT.NONE);
		{
			GridData gd = new GridData();
			widthSpinnerCombo.setLayoutData(gd);
		}

		// höhe
		{
			Label label = new Label(group, SWT.NONE);
			label.setText(GuiMessages.Stileditor_PageLayout_Height + ":");
			GridData gd = new GridData();
			label.setLayoutData(gd);
		}

		heightSpinnerCombo = new SpinnerAndCombo(group, SWT.NONE);
		{
			GridData gd = new GridData();
			heightSpinnerCombo.setLayoutData(gd);
		}

	
		group = new Group(outerGroup, SWT.TOP);
		group.setText(GuiMessages.Stileditor_PageLayout_Margins);
		group.setLayout(new GridLayout(4, false));
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		// Margin top
		{
			Label label = new Label(group, SWT.NONE);
			label.setText(GuiMessages.Stileditor_PageLayout_Margin_Top + ":");
			GridData gd = new GridData();
			label.setLayoutData(gd);
		}
		{
			obenSpinnerCombo = new SpinnerAndCombo(group, SWT.NONE);
			GridData gd = new GridData();
			obenSpinnerCombo.setLayoutData(gd);
		}

		// Margin bottom
		{
			Label label = new Label(group, SWT.NONE);
			label.setText(GuiMessages.Stileditor_PageLayout_Margin_Bottom + ":");
			GridData gd = new GridData();
			gd.horizontalIndent = 50;
			label.setLayoutData(gd);
		}
		{
			untenSpinnerCombo = new SpinnerAndCombo(group, SWT.NONE);
			GridData gd = new GridData();
			untenSpinnerCombo.setLayoutData(gd);
		}

		// Margin left
		{
			labelL = new Label(group, SWT.NONE);
			labelL.setText(GuiMessages.Stileditor_PageLayout_Margin_Left + ": ");
			GridData gd = new GridData();
			labelL.setLayoutData(gd);
		}
		{
			linksSpinnerCombo = new SpinnerAndCombo(group, SWT.NONE);
			GridData gd = new GridData();
			linksSpinnerCombo.setLayoutData(gd);
		}

		// Margin right
		{
			labelR = new Label(group, SWT.NONE);
			labelR.setText(GuiMessages.Stileditor_PageLayout_Margin_Right + ":");
			GridData gd = new GridData();
			gd.horizontalIndent = 50;
			labelR.setLayoutData(gd);

		}
		{
			rechtsSpinnerCombo = new SpinnerAndCombo(group, SWT.NONE);
			GridData gd = new GridData();
			rechtsSpinnerCombo.setLayoutData(gd);
		}
		
		{ 
			Label label = new Label(group, SWT.NONE);
			label.setText(GuiMessages.Stileditor_PageLayout_Layout_Doublesided + ":");
			GridData gd = new GridData();
			label.setLayoutData(gd);
		}
		{
			doublesidedCheckbox = new Button(group, SWT.CHECK);
			GridData gd = new GridData();
			doublesidedCheckbox.setLayoutData(gd);
			
			doublesidedCheckbox.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event e) {
					if (doublesidedCheckbox.getSelection()) {
						labelL.setText(GuiMessages.Stileditor_PageLayout_Margin_Inner + ":");
						labelR.setText(GuiMessages.Stileditor_PageLayout_Margin_Outer + ":");
						for (Control c : evengroup.getChildren()) {
							c.setEnabled(false);
						}
						evengroup.setText("");
						oddgroup.setText(GuiMessages.Stileditor_PageLayout_HeaderFooter_All);
					
					} else { 
						labelL.setText(GuiMessages.Stileditor_PageLayout_Margin_Left + ":");
						labelR.setText(GuiMessages.Stileditor_PageLayout_Margin_Right + ":");
						for (Control c : evengroup.getChildren()) {
							c.setEnabled(true);
						}
						evengroup.setText(GuiMessages.Stileditor_PageLayout_HeaderFooter_Left);
						oddgroup.setText(GuiMessages.Stileditor_PageLayout_HeaderFooter_Right);
					}
				}
		  	});
		}
		 
		// TODO: Die Spinner sollte Einfluss auf die Combobox haben, so dass das
		// Papierformat sich direkt ändert
		/*
		 * MS: derzeit noch unnötig // Bundsteg: { Label label = new
		 * Label(group, SWT.NONE); label.setText("Bundsteg:");// TODO:
		 * GuiMessages GridData gd = new GridData(); label.setLayoutData(gd); }
		 * { GridData gd = new GridData(); bundstegSpinnerCombo = new
		 * SpinnerAndCombo(group, SWT.NONE);
		 * bundstegSpinnerCombo.setLayoutData(gd); }
		 * 
		 * // Bundstegposition: { Label label = new Label(group, SWT.NONE);
		 * label.setText("Bundstegposition:");// TODO: GuiMessages GridData gd =
		 * new GridData(); gd.horizontalIndent = 50; label.setLayoutData(gd); }
		 * { GridData gd = new GridData(); bundstegpositionSpinnerCombo = new
		 * SpinnerAndCombo(group, SWT.NONE);
		 * bundstegpositionSpinnerCombo.setLayoutData(gd); }
		 */

	
	{
		group = createGroup(GuiMessages.Stileditor_PageLayout_HeaderFooter);
		group.setLayout(new GridLayout(3, true));
		
		//TODO: Add boxes for header/footer
		
		evengroup = new Group(group, SWT.TOP);
		evengroup.setText(GuiMessages.Stileditor_PageLayout_HeaderFooter_Left);

		evengroup.setLayout(new GridLayout(3, true));
		
		lohead = new Text(evengroup, SWT.BORDER);
		cohead = new Text(evengroup, SWT.BORDER);
		rohead = new Text(evengroup, SWT.BORDER);
		
		lofoot = new Text(evengroup, SWT.BORDER);
		cofoot = new Text(evengroup, SWT.BORDER);
		rofoot = new Text(evengroup, SWT.BORDER);
		
		
		oddgroup = new Group(group, SWT.TOP);
		oddgroup.setText(GuiMessages.Stileditor_PageLayout_HeaderFooter_Right);
				
		oddgroup.setLayout(new GridLayout(3, true));
		
		lehead = new Text(oddgroup, SWT.BORDER);
		cehead = new Text(oddgroup, SWT.BORDER);
		rehead = new Text(oddgroup, SWT.BORDER);
		
		lefoot = new Text(oddgroup, SWT.BORDER);
		cefoot = new Text(oddgroup, SWT.BORDER);
		refoot = new Text(oddgroup, SWT.BORDER);
	
		headfootpositions= new Text[] { lohead, cohead,	rohead,
				lofoot, cofoot, rofoot, lehead, cehead, rehead, lefoot, cefoot, refoot };
		for (int i = 0; i < headfootpositions.length; i++) {
			headfootpositions[i].addListener(SWT.FOCUSED, new Listener() {
				
				@Override
				public void handleEvent(Event event) {
					activeTextField = (Text) event.widget;
					activeTextFieldPos = activeTextField.getCaretPosition();
				}
			});
		}
		
		Group marks = new Group(group, SWT.TOP);
		marks.setLayoutData(new GridData(GridData.FILL_BOTH));
		//marks.setText(GuiMessages.Fieldinformation_Predefined_Counter);
		marks.setLayout(new FillLayout());
		
		final TabFolder tabFolder = new TabFolder(marks, SWT.BORDER);
		
		TabItem tabItem = new TabItem(tabFolder, SWT.NULL);
	    tabItem.setText("Referenzfelder");//$NON-NLS-N$
	    refFieldItemList = new org.eclipse.swt.widgets.List(tabFolder, SWT.NONE);
	    tabItem.setControl(getTabControl(tabFolder, refFieldItemList, refFieldItemMap));
	    
	    /*
	     * Tab for typographic elements
	     */
	    tabItem = new TabItem(tabFolder, SWT.NULL);
	    tabItem.setText("Typographische Zähler"); //$NON-NLS-N$
	    counterItemList_typographic = new org.eclipse.swt.widgets.List(tabFolder, SWT.NONE);
	    tabItem.setControl(getTabControl(tabFolder, counterItemList_typographic, counterItemMap_typographic));
	 	
	}

	{
		Composite footComp = new Composite(this, SWT.BOTTOM);
		footComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		footComp.setLayout(new GridLayout(4, false));
		Button okButton = new Button(footComp, SWT.NONE);
		okButton.setText(GuiMessages.General_Gui_Okay);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				// write info in maps
				if (pageMaster == null) {
					pageMaster = new PageMaster();
					updatePageMaster();
					pageMasterGroup.add(pageMaster);
				} else {
					updatePageMaster();
				}
				notifyObservers();
				_this.getShell().close();
				}
		});

		Button cancelButton = new Button(footComp, SWT.NONE);
		cancelButton.setText(GuiMessages.General_Gui_Cancel);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				_this.getShell().close();
			}
		});
	}}
	}

	private void fillControls() {
		if (pageMaster == null) {
			pageMaster = new PageMaster();
			pageMasterGroup.add(pageMaster);
			resetControls();
		} else {
			resetControls();
			nameText.setText(pageMaster.getValueOf("name"));
			String w = pageMaster.getValueOf("page-layout-width");
			String h = pageMaster.getValueOf("page-layout-height");
			widthSpinnerCombo.setValue(w);
			heightSpinnerCombo.setValue(h);
	
			if (w.equalsIgnoreCase(Constants.A0_W)
					&& h.equalsIgnoreCase(Constants.A0_H)) {
				setCombo(pageSizeCombo, "A0");
			} else if (w.equalsIgnoreCase(Constants.A1_W)
					&& h.equalsIgnoreCase(Constants.A1_H)) {
				setCombo(pageSizeCombo, "A1");
			} else if (w.equalsIgnoreCase(Constants.A2_W)
					&& h.equalsIgnoreCase(Constants.A2_H)) {
				setCombo(pageSizeCombo, "A2");
			} else if (w.equalsIgnoreCase(Constants.A3_W)
					&& h.equalsIgnoreCase(Constants.A3_H)) {
				setCombo(pageSizeCombo, "A3");
			} else if (w.equalsIgnoreCase(Constants.A4_W)
					&& h.equalsIgnoreCase(Constants.A4_H)) {
				setCombo(pageSizeCombo, "A4");
			} else if (w.equalsIgnoreCase(Constants.A5_W)
					&& h.equalsIgnoreCase(Constants.A5_H)) {
				setCombo(pageSizeCombo, "A5");
			} else if (w.equalsIgnoreCase(Constants.B4_W)
					&& h.equalsIgnoreCase(Constants.B4_H)) {
				setCombo(pageSizeCombo, "B4");
			} else if (w.equalsIgnoreCase(Constants.B5_W)
					&& h.equalsIgnoreCase(Constants.B5_H)) {
				setCombo(pageSizeCombo, "B5");
			} else {
				pageSizeCombo.select(pageSizeCombo.getItemCount() - 1);
			}
	
			obenSpinnerCombo.setValue(pageMaster
					.getValueOf("page-layout-margin-top"));
			untenSpinnerCombo.setValue(pageMaster
					.getValueOf("page-layout-margin-bottom"));
			linksSpinnerCombo.setValue(pageMaster
					.getValueOf("page-layout-margin-left"));
			rechtsSpinnerCombo.setValue(pageMaster
					.getValueOf("page-layout-margin-right"));
			if (pageMaster.getValueOf("page-layout-doublesided").equalsIgnoreCase("true"))
				doublesidedCheckbox.setSelection(true);
			else
				doublesidedCheckbox.setSelection(false);
			doublesidedCheckbox.notifyListeners(SWT.Selection, new Event());
			
			lehead.setText(pageMaster.getValueOf("page-layout-lehead"));
			cehead.setText(pageMaster.getValueOf("page-layout-cehead"));
			rehead.setText(pageMaster.getValueOf("page-layout-rehead"));
			lohead.setText(pageMaster.getValueOf("page-layout-lohead"));
			cohead.setText(pageMaster.getValueOf("page-layout-cohead"));
			rohead.setText(pageMaster.getValueOf("page-layout-rohead"));
			
			lefoot.setText(pageMaster.getValueOf("page-layout-lefoot"));
			cefoot.setText(pageMaster.getValueOf("page-layout-cefoot"));
			refoot.setText(pageMaster.getValueOf("page-layout-refoot"));
			lofoot.setText(pageMaster.getValueOf("page-layout-lofoot"));
			cofoot.setText(pageMaster.getValueOf("page-layout-cofoot"));
			rofoot.setText(pageMaster.getValueOf("page-layout-rofoot"));
		}
		
		int i = 0;
		refFieldItemMap.clear();
		String fieldName;
		ReferenceFieldGroup referenceFieldGroup = RuntimeUtil.getInstance().getCurrentPrintProject().getReferenceFieldGroup();
		if (referenceFieldGroup != null) {
			for (AbstractBaseItem field : referenceFieldGroup.getItems()) {
				fieldName = field.getValueOf("name");
				refFieldItemList.add(fieldName);
				refFieldItemMap.put(i++, new String[] { fieldName,
						"${" + fieldName + "}" });
			}
		} else {
			refFieldItemList.add("Keine vorhanden"); //$NON-NLS-N$
		}
		
		// typographic counters
		i = 0;
		counterItemMap_typographic.clear();
		// Seitenzahl
		counterItemList_typographic.add(counterPageItem[0]);
		counterItemMap_typographic.put(i++, counterPageItem);

		// Spaltenzahl
		counterItemList_typographic.add(counterColumnItem[0]);
		counterItemMap_typographic.put(i++, counterColumnItem);

		// Zeilenummer
		counterItemList_typographic.add(counterRowItem[0]);
		counterItemMap_typographic.put(i++, counterRowItem);

		nameText.setFocus();
		
	}

	private void setCombo(Combo combo, String str) {

		if (str == null)
			return;

		int index = -1;
		for (int i = 0; i < combo.getItems().length; i++)
			if (combo.getItems()[i].equalsIgnoreCase(str)) {
				index = i;
				break;
			}

		if (index > -1) {
			combo.select(index);
		}

	}
	
	private void resetControls() {
		nameText.setText("");
		lehead.setText("");
		cehead.setText("");
		rehead.setText("");
		lohead.setText("");
		cohead.setText("");
		rohead.setText("");
		
		lefoot.setText("");
		cefoot.setText("");
		refoot.setText("");
		lofoot.setText("");
		cofoot.setText("");
		rofoot.setText("");
		
		setCombo(pageSizeCombo, Constants.paperFormat);
		obenSpinnerCombo.setValue(Constants.margin_T);
		untenSpinnerCombo.setValue(Constants.margin_B);
		linksSpinnerCombo.setValue(Constants.margin_L);
		rechtsSpinnerCombo.setValue(Constants.margin_R);
		doublesidedCheckbox.setSelection(Constants.doublesided);
						
		refFieldItemList.removeAll();
		counterItemList_typographic.removeAll();
		
	}

	private void updatePageMaster() {
		pageMaster.setValueOf("name",
				nameText.getText());
		pageMaster.setValueOf("page-layout-width",
				widthSpinnerCombo.getValue());
		pageMaster.setValueOf("page-layout-height",
				heightSpinnerCombo.getValue());
		pageMaster.setValueOf("page-layout-margin-top",
				obenSpinnerCombo.getValue());
		pageMaster.setValueOf("page-layout-margin-bottom",
				untenSpinnerCombo.getValue());
		pageMaster.setValueOf("page-layout-margin-left",
				linksSpinnerCombo.getValue());
		pageMaster.setValueOf("page-layout-margin-right",
				rechtsSpinnerCombo.getValue());
		pageMaster.setValueOf("page-layout-doublesided",
				doublesidedCheckbox.getSelection() ? "true" : "false");
		pageMaster.setValueOf("page-layout-lehead", lehead.getText());
		pageMaster.setValueOf("page-layout-cehead", cehead.getText());
		pageMaster.setValueOf("page-layout-rehead", rehead.getText());
		pageMaster.setValueOf("page-layout-lohead", lohead.getText());
		pageMaster.setValueOf("page-layout-cohead", cohead.getText());
		pageMaster.setValueOf("page-layout-rohead", rohead.getText());
		pageMaster.setValueOf("page-layout-lefoot", lefoot.getText());
		pageMaster.setValueOf("page-layout-cefoot", cefoot.getText());
		pageMaster.setValueOf("page-layout-refoot", refoot.getText());
		pageMaster.setValueOf("page-layout-lofoot", lofoot.getText());
		pageMaster.setValueOf("page-layout-cofoot", cofoot.getText());
		pageMaster.setValueOf("page-layout-rofoot", rofoot.getText());
	}
	
	public PageMaster getPageMaster() {
		return this.pageMaster;
	}
	
	@Override
	public void notifyObservers() {
		for (IObserverControl o : observerList) {
			o.updateControl();
		}
	}

	@Override
	public void registerObserver(IObserverControl observer) {
		observerList.add(observer);
	}

	@Override
	public void removeObserver(IObserverControl observer) {
		observerList.remove(observer);
	}

	@Override
	public void setPrintProject(PrintProject pProject) {
		printProject = pProject;
	}

	public void setPageMasterGroup(AbstractBaseGroup pageMasterGroup) {
		this.pageMasterGroup = (PageMasterGroup) pageMasterGroup;
	}
	
	private Group createGroup(String title) {
		Group group = new Group(this, SWT.NONE);
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		group.setText(title);
		return group;
	}
	
	private Control getTabControl (TabFolder tabFolder, final org.eclipse.swt.widgets.List l, final Map<Integer, String[]> m) {
	    l.addListener(SWT.MouseDoubleClick, new Listener() {
    	@Override
    	public void handleEvent(Event event) {
    		Text t = activeTextField;
    		
    		// We test, whether the cursor is inside {}. Then no insertion is allowed.
    		int cursorpos = activeTextFieldPos;
			
	    	int lcb = t.getText(0,cursorpos).lastIndexOf("{"); 
			if (lcb > -1) { // { found -> look for corresponding }
				if (t.getText(lcb, t.getText().length()).indexOf("}") > cursorpos - lcb - 1)
					return;
			}
    		
			lcb = t.getText(0,cursorpos).lastIndexOf("&"); 
			if (lcb > -1) { // & found -> look for corresponding ;
				if (t.getText(lcb, t.getText().length()).indexOf(";") > cursorpos - lcb - 1 && lcb < cursorpos )
					return;
			}
    		
			t.insert((String) m.get(l.getSelectionIndex())[1]);
	    	}
	    });
	
	    return l;
	}
	
	public void setInput(PageMaster pm) {
		pageMaster = pm;
		//updateFormatCombo();

		/*if (pm == null) {// TODO: Check why this is not working at all. Do we
							// need resetControls before fillControls?
			resetControls();
		} else*/
			fillControls();
	}
}